package com.zabra.zl.ui.regist;

import android.app.Dialog;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.data.SimpleBean;
import com.zabra.zl.ui.regist.contract.RegistContract;
import com.zabra.zl.ui.regist.presenter.RegistPresenter;
import com.zabra.zl.utils.CountDownTimerUtils;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.MaptoJsonUtils;
import com.zabra.zl.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

public class RegistActivity extends BaseMvpActivity<RegistPresenter> implements RegistContract.View {


    @BindView(R.id.user_phone)
    EditText userPhone;
    @BindView(R.id.user_name)
    EditText userName;
    @BindView(R.id.user_pwd)
    EditText userPwd;
    @BindView(R.id.recommend)
    EditText recommend;
    @BindView(R.id.vercode)
    EditText vercode;
    @BindView(R.id.get_code)
    TextView getCode;
    @BindView(R.id.agree)
    CheckBox agree;
    @BindView(R.id.look_agree)
    TextView lookAgree;
    @BindView(R.id.bt_regist)
    TextView btRegist;
    @BindView(R.id.bt_login)
    TextView btLogin;
    private String strTel, strName, strPwd, strRecomment, strCode;
    private Dialog dialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_regist;
    }

    @Override
    public void initView() {
        mPresenter = new RegistPresenter();
        mPresenter.attachView(this);
    }

    @Override
    public void showLoading() {
        dialog = DialogUtils.showDialog_progressbar(this);
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onSuccess(SimpleBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(getCode, 60000, 1000);
            mCountDownTimerUtils.start();
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @Override
    public void onSuccessRegist(SimpleBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            ToastUtil.showToast(this, "注册成功马上登录");
            Intent intent = new Intent();
            intent.putExtra("tel", strTel);
            setResult(1, intent);
            finish();
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @OnCheckedChanged(R.id.agree)
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            //同意协议
            btRegist.setAlpha(1);
            btRegist.setEnabled(true);
        } else {
            btRegist.setAlpha(0.5f);
            btRegist.setEnabled(false);
        }
    }

    @OnClick({R.id.get_code, R.id.look_agree, R.id.bt_regist, R.id.bt_login})
    public void onViewClicked(View view) {
        strTel = userPhone.getText().toString().trim();
        strName = userName.getText().toString().trim();
        strPwd = userPwd.getText().toString().trim();
        strRecomment = recommend.getText().toString().trim();
        strCode = vercode.getText().toString().trim();
        switch (view.getId()) {
            case R.id.get_code:
                //获取验证码
                if (TextUtils.isEmpty(strTel) || TextUtils.isEmpty(strName) || TextUtils.isEmpty(strPwd)) {
                    ToastUtil.showToast(this, "请完善信息");
                } else {
                    keyList.add("phone");
                    keyList.add("loginName");
                    keyList.add("loginPwd");
                    valueList.add(strTel);
                    valueList.add(strName);
                    valueList.add(strPwd);
                    mPresenter.getCode(MaptoJsonUtils.Simple(keyList, valueList));
                    keyList.clear();
                    valueList.clear();
                }
                break;
            case R.id.look_agree:
                //查看协议
                break;
            case R.id.bt_regist:
                //立即注册
                if (TextUtils.isEmpty(strTel) || TextUtils.isEmpty(strName) || TextUtils.isEmpty(strPwd) || TextUtils.isEmpty(strCode)) {
                    ToastUtil.showToast(this, "请完善信息");
                } else {
                    keyList.add("phone");
                    keyList.add("loginName");
                    keyList.add("loginPwd");
                    keyList.add("agentCode");
                    keyList.add("yzm");
                    valueList.add(strTel);
                    valueList.add(strName);
                    valueList.add(strPwd);
                    valueList.add(strRecomment);
                    valueList.add(strCode);
                    mPresenter.getData(MaptoJsonUtils.Simple(keyList, valueList));
                    keyList.clear();
                    valueList.clear();
                }
                break;
            case R.id.bt_login:
                //去登录
                finish();
                break;
        }
    }
}
